--[[----------------------------------------------------------------------------
    This file is part of yalps.

    yalps is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    yalps is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with yalps.  If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
--[[----------------------------------------------------------------------------
    Defines this plugin post process actions
------------------------------------------------------------------------------]]

local LrView = import 'LrView'
local LrFunctionContext = import 'LrFunctionContext'
local LrDialogs = import 'LrDialogs'
local LrPathUtils = import 'LrPathUtils'
local LrFileUtils = import 'LrFileUtils'

require 'CommandRunner.lua'

local RunCommandAction = {}

return {
	-- Definition of the user interface shown 
	sectionForFilterInDialog = function( viewFactory, propertyTable )
		-- Default values
		--propertyTable.exportCommandLine = "";
		return {
			title = "Run command",
			synopsis = LrView.bind { key = 'exportCommandLine', object = propertyTable }, -- show property value when collapsed
			viewFactory:row {
				spacing = viewFactory:control_spacing(),
				viewFactory:static_text {
					title = LOC "$$$/yalps/runme/postprocess/dialogs/command/text-caption=Command line to run",
					truncation = "tail",
					alignment = "left",
				},
				viewFactory:edit_field {
					value = LrView.bind 'exportCommandLine', -- bound to property
					tooltip = LOC "$$$/yalps/runme/postprocess/dialogs/command/field-tooltip=Enter the command line you wish to run",
					width_in_chars = 60,
					height_in_lines = 4,
				},					
			}
		}
	end,
	postProcessRenderedPhotos = function( functionContext, filterContext ) 
		for sourceRendition, renditionToSatisfy in filterContext:renditions() do
			-- Wait for the upstream task to finish its work on this photo.
			local success, pathOrMessage = sourceRendition:waitForRender()
			if success then
				if (not LrFileUtils.exists(pathOrMessage)) then
					LrDialogs.message(pathOrMessage .. " doesn't exist", nil, "critical" ) 
				else
					-- see file:///D:/@desarrollo/lightroom2/sdk/API%20Reference/modules/LrPathUtils.html#LrPathUtils.getStandardFilePath 'home', 'temp', 'desktop', 'appPrefs', 'pictures', 'documents'
					local params = {
						PHOTO_FULLPATH = pathOrMessage,
						PHOTO_FILENAME = LrPathUtils.leafName(pathOrMessage),
					}
					CommandRunner.run(filterContext.propertyTable.exportCommandLine, params)				
				end
			else
				LrDialogs.message("Error rendering photo", pathOrMessage, "critical" ) 
			end
		end
	end,
}